package characters;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Animation {
    private final Map<String, AnimationData> animations = new HashMap<>();
    private AnimationData currentAnimation;
    private long animationStartTime;
    private int currentLoopCount = 0;

    // 动画数据内部类
    private static class AnimationData {
        ImageIcon gifIcon;
        boolean loops;
        long durationMs;
        int totalFrames;
        int currentFrame = 0;

        AnimationData(ImageIcon gifIcon, boolean loops, long durationMs) {
            this.gifIcon = gifIcon;
            this.loops = loops;
            this.durationMs = durationMs;
            // 简单估算帧数(实际应该解析GIF帧信息)
            this.totalFrames = (int)(durationMs / 100); // 假设每帧100ms
        }
    }

    public void addAnimation(String state, String gifPath, boolean loops, long durationMs) {
        try {
            URL resource = getClass().getResource(gifPath);
            if (resource == null) {
                throw new IOException("资源不存在: " + gifPath);
            }

            ImageIcon gifIcon = new ImageIcon(resource);
            animations.put(state, new AnimationData(gifIcon, loops, durationMs));
        } catch (Exception e) {
            System.err.println("加载动画失败: " + e.getMessage());
            // 创建占位动画
            BufferedImage placeholder = createPlaceholderImage();
            animations.put(state, new AnimationData(new ImageIcon(placeholder), loops, durationMs));
        }
    }

    public void play(String state) {
        if (animations.containsKey(state)) {
            currentAnimation = animations.get(state);
            animationStartTime = System.currentTimeMillis();
            currentAnimation.currentFrame = 0;
            currentLoopCount = 0;
        }
    }

    public void update() {
        if (currentAnimation != null) {
            // 更新当前帧(简化处理，实际应该根据GIF帧时间)
            long elapsed = System.currentTimeMillis() - animationStartTime;
            currentAnimation.currentFrame = (int)(elapsed / 100) % currentAnimation.totalFrames;

            // 检查非循环动画是否完成
            if (!currentAnimation.loops && elapsed >= currentAnimation.durationMs) {
                currentAnimation = null;
            }
        }
    }

    public BufferedImage getCurrentFrame() {
        if (currentAnimation == null) return null;

        Image image = currentAnimation.gifIcon.getImage();
        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        // 转换Image到BufferedImage
        BufferedImage bufferedImage = new BufferedImage(
                image.getWidth(null),
                image.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.drawImage(image, 0, 0, null);
        g2d.dispose();

        return bufferedImage;
    }

    public boolean isAnimationFinished() {
        // 循环动画永远不会"完成"
        if (currentAnimation == null || currentAnimation.loops) {
            return false;
        }

        // 检查非循环动画是否播放完毕
        long elapsed = System.currentTimeMillis() - animationStartTime;
        return elapsed >= currentAnimation.durationMs;
    }

    public String getCurrentState() {
        return
                currentAnimation != null ? currentAnimation.gifIcon.getDescription() : null;
    }

    private BufferedImage createPlaceholderImage() {
        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(Color.RED);
        g2d.fillRect(0, 0, 100, 100);
        g2d.dispose();
        return img;
    }
}